package com.hps.notifymsg.pay.service.impl;

import com.hps.notifymsg.pay.dao.AccountPayDao;
import com.hps.notifymsg.pay.entity.AccountPay;
import com.hps.notifymsg.pay.service.AccountPayService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author heps
 * @date 2019/12/14 15:38
 */
@Slf4j
@Service
public class AccountPayServiceImpl implements AccountPayService {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private AccountPayDao accountPayDao;

    @Override
    @Transactional
    public AccountPay insertAccountPay(AccountPay accountPay) {
        int success = accountPayDao.insertAccountPay(accountPay.getId(), accountPay.getAccountNo(), accountPay.getPayAmount(), "success");
        if (success > 0) {
            accountPay.setResult("success");
            rocketMQTemplate.convertAndSend("topic_notifymsg", accountPay);
            return accountPay;
        }
        return null;
    }

    @Override
    public AccountPay getAccountPay(String txNo) {
        AccountPay accountPay = accountPayDao.findByIdTxNo(txNo);
        return accountPay;
    }
}
